package com.central.apps.handler.project;

import com.central.app.common.base.BaseHandler;
import com.central.app.common.util.DateUtils;
import com.central.app.service.HandlerService;
import com.central.apps.service.ProjectChangeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 处理变更明细管理业务逻辑
 *
 * @author lidachao
 * @date 2023/3/11
 */
@Component
@Slf4j
public class ProjectChangeTaskDetailHandler extends BaseHandler {

    @Autowired
    private HandlerService handlerService;
    @Autowired
    private ProjectChangeService projectChangeService;

    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        if ("delTaskDeli".equals(type)) {
            // 删除任务交付物
            Integer id = MapUtils.getInteger(params, "id");
            handlerService.updateBySql("delete from gq_taskdelichg where id = " + id);
        }
        return null;
    }

    @Override
    public String validateSave(String app, String objName, Map<String, Object> data) throws Exception {
        if ("gq_pctaskchg".equals(objName)) {
            String startTime = MapUtils.getString(data, "startTime");
            String endTime = MapUtils.getString(data, "endTime");

            boolean bool = DateUtils.isWeekEnd(startTime);
            if (bool) {
                return "开始日期不能为周六日!";
            }
            bool = DateUtils.isWeekEnd(endTime);
            if (bool) {
                return "结束日期不能为周六日!";
            }
        }
        return null;
    }

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        if ("gq_pctaskchg".equals(objName)) {
            Integer id = MapUtils.getInteger(data, "id");
            Integer proId = MapUtils.getInteger(data, "proid");
            Integer pcId = MapUtils.getInteger(data, "pcid");
            Integer parentId = MapUtils.getInteger(data, "parentId");
            Integer level = 1;
            Integer parentLevel = 0;

            if (parentId != 0) {
                List<Map<String, Object>> parentTaskList = handlerService.findBySql("select level from gq_pctaskchg where pcid =" + pcId + " and id = " + parentId);
                parentLevel = MapUtils.getInteger(parentTaskList.get(0), "level");
                level = parentLevel + 1;

                // 更新子级层级
                handlerService.updateBySql("update gq_pctaskchg set level = " + level + ", parentLevel = " + parentLevel + " where id = " + id);
            }

            // 重新计算工期
            String startTime = MapUtils.getString(data, "startTime");
            String endTime = MapUtils.getString(data, "endTime");
            long duration = DateUtils.daysBetweenExpWeekEnd(startTime, endTime);
            handlerService.updateBySql("update gq_pctaskchg set duration = " + duration + " where id = " + id);

            // 重新整理记录id
            projectChangeService.updateRecordId(proId, pcId);
            // 重新整理父子关系
            projectChangeService.updParentId(proId, pcId);
            // 更新前置任务信息
            projectChangeService.updPredecessors(proId, pcId);
        }
    }
}
